//调入数据库
const db = require('../db');
//登录路由的处理函数
exports.login = (req, res) => {
  const sql = `select * from bill.pass where user=?`;

  db.query(sql, req.body.user, (err, results) => {
    if (err) return res.sd(err.message);
    if (results.length !== 1) return res.sd('登录失败');
    // 删除头像和密码后的用户信息

    res.send({
      status: 0,
      message: '登录成功',
    });
  });
};
// 注册账号
exports.reg = (req, res) => {
  const sql = `select * from bill.pass where user=?`;

  db.query(sql, req.body.user, (err, results) => {
    //sql错误信息
    if (err) return res.sd(err.message);
    if (results.length !== 0) return res.sd('用户名已注册');
    //加密密码
    const pwd = req.body.word;
    const sql = `insert into bill.pass set?`;
    db.query(sql, { user: req.body.user, word: pwd }, (err, results) => {
      if (err)
        return res.send({
          status: 1,
          message: err.message,
        });

      //通过是否影响行数判断是否添加成功
      if (results.affectedRows !== 1)
        return res.send({
          status: 1,
          message: '用户注册失败！',
        });
      res.send({
        status: 0,
        message: '用户注册成功！',
      });
    });
  });
};
